package com.shop.front.util.tencent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.front.property.StaticConstProps;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2023/9/4 11:21
 * @Version 1.0
 */
public class IdCardVerification {
    protected static  final String API_SECRETID = StaticConstProps.secretId;
    protected static final String API_SECRET_KEY = StaticConstProps.secretKey;
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    protected Logger logger = LoggerFactory.getLogger(getClass());



    public static void main(String[] args) throws Exception {
        VerifyIdCard("430224*******************", "王**");
        System.out.println(JSON.toJSON(identityCard18("4302241*****************")));
    }

    /**
     * 验证身份信息是否正确
     *
     * @param idCard
     * @param name
     */
    public static boolean VerifyIdCard(String idCard, String name) {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential(API_SECRETID, API_SECRET_KEY);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            FaceidClient client = new FaceidClient(cred, "", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            IdCardVerificationRequest req = new IdCardVerificationRequest();
            req.setIdCard(idCard);
            req.setName(name);
            // 返回的resp是一个IdCardVerificationResponse的实例，与请求对象对应
            IdCardVerificationResponse resp = client.IdCardVerification(req);
            // 输出json格式的字符串回包
            String str = IdCardVerificationResponse.toJsonString(resp);
            JSONObject object = JSON.parseObject(str);
            if (object.getInteger("Result") == 0 && "姓名和身份证号一致".equals(object.getString("Description"))){
                return true;
            }
            // TODO: 2023/6/25  错误身份证号
            System.out.println(str);
            return false;
        } catch (TencentCloudSDKException e) {
            // TODO: 2023/6/25 请求接口异常
            System.out.println(e.toString());
            return false;
        }
    }

    /**
     * 18位身份证获取性别和年龄
     * @param CardCode
     * @return
     * @throws Exception
     */
    public static Map<String, Object> identityCard18(String CardCode) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        // 得到年份
        String year = CardCode.substring(6).substring(0, 4);
        // 得到月份
        String month = CardCode.substring(10).substring(0, 2);
        //得到日
        //String day=CardCode.substring(12).substring(0,2);
        String sex;
        // 判断性别
        if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {
            sex = "女";
        } else {
            sex = "男";
        }
        // 得到当前的系统时间
        Date date = new Date();
        // 当前年份
        String currentYear = format .format(date).substring(0, 4);
        // 月份
        String currentMonth = format.format(date).substring(5, 7);
        int age = 0;
        // 当前月份大于用户出身的月份表示已过生日
        if (Integer.parseInt(month) <= Integer.parseInt(currentMonth)) {
            age = Integer.parseInt(currentYear) - Integer.parseInt(year) + 1;
        } else {
            // 当前用户还没过生日
            age = Integer.parseInt(currentYear) - Integer.parseInt(year);
        }
        map.put("sex", sex);
        map.put("age", age);
        return map;
    }

    /**
     * 15位身份证获取性别和年龄
     * @param card
     * @return
     * @throws Exception
     */
    public static Map<String, Object> identityCard15(String card) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        //年份
        String year = "19" + card.substring(6, 8);
        //月份
        String yue = card.substring(8, 10);
        //日
        //String day=card.substring(10, 12);
        String sex;
        if (Integer.parseInt(card.substring(14, 15)) % 2 == 0) {
            sex = "女";
        } else {
            sex = "男";
        }
        // 得到当前的系统时间
        Date date = new Date();
        //当前年份
        String currentYear = format.format(date).substring(0, 4);
        //月份
        String currentMonth = format.format(date).substring(5, 7);
        //String fday=format.format(date).substring(8,10);
        int age = 0;
        //当前月份大于用户出身的月份表示已过生日
        if (Integer.parseInt(yue) <= Integer.parseInt(currentMonth)) {
            age = Integer.parseInt(currentYear) - Integer.parseInt(year) + 1;
        } else {
            // 当前用户还没过生日
            age = Integer.parseInt(currentYear) - Integer.parseInt(year);
        }
        map.put("sex", sex);
        map.put("age", age);
        return map;
    }


}
